package com.ssm.controller;




import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.druid.support.json.JSONUtils;


import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.pojo.*;
import com.ssm.service.*;


import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.awt.SystemColor.info;


@Controller
@RequestMapping(value="/dev/")
public class DevUserController {


	@Autowired
    private DevUserServiceDao devUserServiceDao;
	@Autowired
    private DataDictionaryServiceDao DataDictionaryServiceDao;
    @Autowired
    private AppCategoryServiceDao AppCategoryServiceDao;
    @Autowired
    private com.ssm.service.AppInfoServiceDao AppInfoServiceDao;
    @Autowired
    private com.ssm.service.AppVersionServiceDao AppVersionServiceDao;
	/*
	  开发者登陆页面
	 */
	@RequestMapping(value="/devlogin")
	public String login(){
		return "devlogin";
	}
  /*
      app维护页面
    */
   @RequestMapping(value="/flatform/app/list")
   public String devList(@RequestParam(required=true,defaultValue="1") Integer page, HttpServletRequest request, Model model,HttpSession session){
       List<DataDictionary> ADictionaries = DataDictionaryServiceDao.selectByATName();//app状态
       List<DataDictionary> SDictionaries = DataDictionaryServiceDao.selectBySTName();//所属平台
       model.addAttribute("statusList",ADictionaries);
       model.addAttribute("flatFormList",SDictionaries);
       List<AppCategory> OneCategories = AppCategoryServiceDao.selectByPId(null);//一级分类
       model.addAttribute("categoryLevel1List",OneCategories);

       PageHelper.startPage(page,2);
       List<com.ssm.pojo.AppInfo> appInfo=null;

       System.out.print(session.getAttribute("querySoftwareName"));
       if(request.getParameter("querySoftwareName")!=""&& request.getParameter("querySoftwareName")!=null
               ||session.getAttribute("querySoftwareName")!=null
               ||request.getParameter("queryStatus")!=""&& request.getParameter("queryStatus")!=null
               ||session.getAttribute("queryStatus")!=null
               ||request.getParameter("queryFlatformId")!=""&& request.getParameter("queryFlatformId")!=null
               ||session.getAttribute("queryFlatformId")!=null
               ||request.getParameter("queryCategoryLevel1")!=""&& request.getParameter("queryCategoryLevel1")!=null
               ||session.getAttribute("queryCategoryLevel1")!=null
               ||request.getParameter("queryCategoryLevel2")!=""&& request.getParameter("queryCategoryLevel2")!=null
               ||session.getAttribute("queryCategoryLevel2")!=null
               ||request.getParameter("queryCategoryLevel3")!=""&& request.getParameter("queryCategoryLevel3")!=null
               ||session.getAttribute("queryCategoryLevel3")!=null){


           String querySoftwareName=request.getParameter("querySoftwareName");

           if (querySoftwareName==null){
               querySoftwareName=null;
               if(session.getAttribute("querySoftwareName")!=null){
                  querySoftwareName=(String)session.getAttribute("querySoftwareName");
               }
           }else if(querySoftwareName==""){
               querySoftwareName=null;
               session.removeAttribute("querySoftwareName");
           }else{
               session.setAttribute("querySoftwareName",querySoftwareName);
           }

           String queryStatus=request.getParameter("queryStatus");//返回的是个空字符串"",不能parseInt()
           if(queryStatus==""){
               queryStatus="0";
               session.removeAttribute("queryStatus");
           }else if(queryStatus==null){
                queryStatus="0";
                if(session.getAttribute("queryStatus")!=null){
                   queryStatus=(String)session.getAttribute("queryStatus");
               }
           }else{
               session.setAttribute("queryStatus",queryStatus);
           }

           String queryFlatformId=request.getParameter("queryFlatformId");
           if(queryFlatformId==""){
               queryFlatformId="0";
               session.removeAttribute("queryFlatformId");
           }else if(queryFlatformId==null){
                 queryFlatformId="0";
                 if(session.getAttribute("queryFlatformId")!=null){
                   queryFlatformId=(String)session.getAttribute("queryFlatformId");
               }
           }else{
               session.setAttribute("queryFlatformId",queryFlatformId);
           }

           String queryCategoryLevel1=request.getParameter("queryCategoryLevel1");
           if(queryCategoryLevel1==""){
               queryCategoryLevel1="0";
               session.removeAttribute("queryCategoryLevel1");
           }else if(queryCategoryLevel1==null){
               queryCategoryLevel1="0";
               if(session.getAttribute("queryCategoryLevel1")!=null){
                   queryCategoryLevel1=(String)session.getAttribute("queryCategoryLevel1");
               }
           }else{
               session.setAttribute("queryCategoryLevel1",queryCategoryLevel1);
           }

           String queryCategoryLevel2=request.getParameter("queryCategoryLevel2");
           if(queryCategoryLevel2==""){
               queryCategoryLevel2="0";
               session.removeAttribute("queryCategoryLevel2");
           }else if(queryCategoryLevel2==null){
               queryCategoryLevel2="0";
               if(session.getAttribute("queryCategoryLevel2")!=null){
                   queryCategoryLevel2=(String)session.getAttribute("queryCategoryLevel2");
               }
           }else{
               session.setAttribute("queryCategoryLevel2",queryCategoryLevel2);
           }

           String queryCategoryLevel3=request.getParameter("queryCategoryLevel3");
           if(queryCategoryLevel3==""){
               queryCategoryLevel3="0";
               session.removeAttribute("queryCategoryLevel3");
           }else if(queryCategoryLevel3==null){
              queryCategoryLevel3="0";
              if(session.getAttribute("queryCategoryLevel3")!=null){
                   queryCategoryLevel3=(String)session.getAttribute("queryCategoryLevel3");
               }
           }else{
               session.setAttribute("queryCategoryLevel3",queryCategoryLevel3);
           }

           appInfo = AppInfoServiceDao.selectBySTCType(querySoftwareName, Integer.parseInt(queryStatus)
                   , Integer.parseInt(queryFlatformId), Integer.parseInt(queryCategoryLevel1)
                   , Integer.parseInt(queryCategoryLevel2), Integer.parseInt(queryCategoryLevel3));
       }else {
           System.out.println("--------------------------------------------------------------222222222222222222222222");
           appInfo = AppInfoServiceDao.selectBySTCType(null, 0, 0, 0, 0, 0);
       }


       PageInfo<AppInfo> pages=new PageInfo<AppInfo>(appInfo);
       model.addAttribute("appInfoList",appInfo);
       model.addAttribute("pages",pages);
       return "developer/appinfolist";
   }
   /*
   二级分类根据一级分类联动
    */
   @ResponseBody
   @RequestMapping(value="/flatform/app/categorylevellist.json")
   public   List<AppCategory> twoChang(HttpServletRequest request){
       String parentId=request.getParameter("pid");
       List<AppCategory> TwoCategories = AppCategoryServiceDao.selectByTTPId(parentId);

       return TwoCategories;
   }
   /*********************************************************************************************************************/

	/*
	登陆开发者主页面
	 */
	@RequestMapping(value=("/dologin"))
	public String doLogin(HttpServletRequest request,HttpSession session){
		String devCode=request.getParameter("devCode");
		String devPassword=request.getParameter("devPassword");
		DevUser devUserSession=devUserServiceDao.devLoginCheck(devCode, devPassword);
		if(devUserSession==null){
			request.setAttribute("deng",0);
			return "devlogin";
		}
		session.setAttribute("devUserSession",devUserSession);
		return "developer/main";
	}

	/*
	开发者注销
	 */
	@RequestMapping(value = "/logout")
	public String logout(HttpSession session){
        session.removeAttribute("devUserSession");
		return "devlogin";
	}

/*************************************************************************************************************/

/*
查看详细的单条app信息页面
 */

@RequestMapping(value = "/flatform/app/appview")
public String appview(HttpServletRequest request,Model model,HttpSession session){
    String id=request.getParameter("id");
    AppInfo appInfo = AppInfoServiceDao.selectOneById(Integer.parseInt(id));
    List<AppVersion> appVersionList = AppVersionServiceDao.selectByAID(Integer.parseInt(id));
    model.addAttribute("appInfo",appInfo);
    model.addAttribute("appVersionList",appVersionList);
    return "developer/appinfoview";
}

/*
删除应用及相关版本信息
 */
    @ResponseBody
    @RequestMapping(value="/flatform/app/delapp.json")
    public  Object delapp(HttpServletRequest request){
        String id=request.getParameter("id");
        int i = AppInfoServiceDao.delById(Integer.parseInt(id));
        int i1 = AppVersionServiceDao.delByAID(Integer.parseInt(id));
        if(i>0 || i1>0){
            return JSONArray.toJSONString("true");
        }else {
            return JSONArray.toJSONString("false");
        }

    }

    /*
    上架下架操作
     */
    @ResponseBody
    @RequestMapping(value="/flatform/app/sale.json")
    public Object sale(HttpServletRequest request){
        String id=request.getParameter("id");
        String status=request.getParameter("status");
        int i=0;
        if (status.equals("4")){
            i = AppInfoServiceDao.updateBySId(Integer.parseInt(status),Integer.parseInt(id));
        }else if(status.equals("5")){
            i = AppInfoServiceDao.updateBySId(Integer.parseInt(status),Integer.parseInt(id));
        }

        if(i>0){
            return JSONArray.toJSONString("success");
        }else {
            return JSONArray.toJSONString("failed");
        }

    }


/*********************************************************************************************************************/
    /*
    新增app基础信息
     */
    @RequestMapping(value = "/flatform/app/appinfoadd")
    public String appinfoadd(HttpServletRequest request,Model model,HttpSession session){

        return "developer/appinfoadd";
    }

    /*
    动态加载所属平台列表
     */
    @ResponseBody
    @RequestMapping(value = "/flatform/app/datadictionarylist.json")
    public Object datadictionarylist(HttpServletRequest request,Model model,HttpSession session){
        List<DataDictionary> dataDictionaries = DataDictionaryServiceDao.selectBySTName();
        return JSONArray.toJSONString(dataDictionaries);
    }

    /*
    动态加载一级分类
     */
    @ResponseBody
    @RequestMapping(value = "/flatform/app/dcategorylevellist.json")
    public Object dcategorylevellist(HttpServletRequest request,Model model,HttpSession session){
        String pid= request.getParameter("pid");
        System.out.println("------------------------------------------------------");
        System.out.println(pid);
        List<AppCategory> appCategories =null;
        if (pid==""){
            appCategories= AppCategoryServiceDao.selectByPId(null);
        }else {
            appCategories= AppCategoryServiceDao.selectByTTPId(pid);
        }
        return JSONArray.toJSONString(appCategories);
    }

    /*
     验证apkName是否存在
     */
    @ResponseBody
    @RequestMapping(value = "/flatform/app/apkexist.json")
    public Object apkexist(HttpServletRequest request,Model model,HttpSession session){
        String APKName=request.getParameter("APKName");
        AppInfo appInfo = AppInfoServiceDao.selectByAName(APKName);
        return JSONArray.toJSONString(appInfo);
    }

    /*
    新增保存操作
     */
    @RequestMapping(value="/flatform/app/appinfoaddsave",method= RequestMethod.POST)
    public String appinfoaddsave(HttpServletRequest request,Model model,
                          @RequestParam(value ="idPath", required = false)MultipartFile attach) {
        String idPath=null;
        System.out.println("-------------------------------------------------------------11111");
        System.out.println(attach);
        if(!attach.isEmpty()){
            //statics固定名称
            String path = request.getSession().getServletContext().getRealPath("statics"+File.separator+"uploadfiles");
            String oldFileName = attach.getOriginalFilename();//原文件名
            String prefix= FilenameUtils.getExtension(oldFileName);
            int filesize = 500000;

            if(attach.getSize()>filesize){
                return "developer/appinfoadd";
            }else if(prefix.equalsIgnoreCase("jpg")||prefix.equalsIgnoreCase("png")
                    ||prefix.equalsIgnoreCase("jpeg")||prefix.equalsIgnoreCase("pneg")){
                String fileName = System.currentTimeMillis()+ RandomUtils.nextInt(1000000)+"_Personal.jpg";
                File targetFile = new File(path, fileName);
                if(!targetFile.exists()){
                    targetFile.mkdirs();
                }
                //保存
                try {
                    attach.transferTo(targetFile);
                    System.out.println("上传成功");
                } catch (Exception e) {
                    e.printStackTrace();
                    request.setAttribute("uploadFileError", " * 上传失败！");
                    return "useradd";
                }
               /* idPath = path+ File.separator+fileName;
                System.out.println(idPath);*/
                idPath ="statics"+"/"+"uploadfiles"+"/"+fileName;
            }else{
                request.setAttribute("uploadFileError", " * 上传图片格式不正确");
                return "developer/appinfoadd";
            }

            String softwareName=request.getParameter("softwareName");
            String APKName=request.getParameter("APKName");
            String supportROM=request.getParameter("supportROM");
            String interfaceLanguage=request.getParameter("interfaceLanguage");
            String softwareSize=request.getParameter("softwareSize");
            String downloads=request.getParameter("downloads");
            String flatformId=request.getParameter("flatformId");
            String categoryLevel1=request.getParameter("categoryLevel1");
            String categoryLevel2=request.getParameter("categoryLevel2");
            String categoryLevel3=request.getParameter("categoryLevel3");
            String appInfo=request.getParameter("appInfo");


            AppInfo info=new AppInfo();
            info.setSoftwarename(softwareName);
            info.setApkname(APKName);
            info.setSupportrom(supportROM);
            info.setInterfacelanguage(interfaceLanguage);
            info.setSoftwaresize(BigDecimal.valueOf(Double.valueOf(softwareSize)));
            info.setAppinfo(appInfo);
            info.setFlatformid(Long.parseLong(flatformId));
            info.setCategorylevel3(Long.parseLong(categoryLevel3));
            info.setDownloads(Long.parseLong(downloads));
            info.setCategorylevel1(Long.parseLong(categoryLevel1));
            info.setCategorylevel2(Long.parseLong(categoryLevel2));
            info.setLogolocpath(idPath);
            info.setLogopicpath(idPath);
            int i = AppInfoServiceDao.insertAInfo(info);
            if (i>0){
                System.out.println("-----------------------------------------------------------睡觉啊");
            }
            return "redirect:/dev/flatform/app/list";
        }else{
            System.out.println("他是空的");
            return "developer/appinfoadd";
        }

    }

    /**************************************************************************************************************/
    /*
    修改页面
     */
    @RequestMapping(value = "/flatform/app/appinfomodify")
    public String appinfomodify(HttpServletRequest request,Model model,HttpSession session){
        String id = request.getParameter("id");
        AppInfo info = AppInfoServiceDao.selectOneById(Integer.parseInt(id));
        model.addAttribute("appInfo",info);
        return "developer/appinfomodify";
    }

     /*
     修改保存
      */
     @RequestMapping(value="/flatform/app/appinfomodifysave",method= RequestMethod.POST)
     public String appinfomodifysave(HttpServletRequest request,Model model,
                                  @RequestParam(value ="idPath", required = false)MultipartFile attach) {
         String idPath=null;
         System.out.println("-------------------------------------------------------------11111");
         System.out.println(attach);
         String id=request.getParameter("id");
         if(!attach.isEmpty()){
             //statics固定名称
             String path = request.getSession().getServletContext().getRealPath("statics"+File.separator+"uploadfiles");
             String oldFileName = attach.getOriginalFilename();//原文件名
             String prefix= FilenameUtils.getExtension(oldFileName);
             int filesize = 500000;

             if(attach.getSize()>filesize){
                 return "redirect:/dev/flatform/app/appinfomodify?id="+id;
             }else if(prefix.equalsIgnoreCase("jpg")||prefix.equalsIgnoreCase("png")
                     ||prefix.equalsIgnoreCase("jpeg")||prefix.equalsIgnoreCase("pneg")){
                 String fileName = System.currentTimeMillis()+ RandomUtils.nextInt(1000000)+"_Personal.jpg";
                 File targetFile = new File(path, fileName);
                 if(!targetFile.exists()){
                     targetFile.mkdirs();
                 }
                 //保存
                 try {
                     attach.transferTo(targetFile);
                     System.out.println("上传成功");
                 } catch (Exception e) {
                     e.printStackTrace();
                     request.setAttribute("uploadFileError", " * 上传失败！");
                     return "useradd";
                 }
                /* idPath = path+ File.separator+fileName;*/

                 idPath ="statics"+"/"+"uploadfiles"+"/"+fileName;
             }else{
                 request.setAttribute("uploadFileError", " * 上传图片格式不正确");
                 return "redirect:/dev/flatform/app/appinfomodify?id="+id;
             }
         }
         if (request.getParameter("status")!=null){
             String status=request.getParameter("status");
             AppInfoServiceDao.updateBySId(Integer.parseInt(status),Integer.parseInt(id));
         }
         String softwareName=request.getParameter("softwareName");
         String APKName=request.getParameter("APKName");
         String supportROM=request.getParameter("supportROM");
         String interfaceLanguage=request.getParameter("interfaceLanguage");
         String softwareSize=request.getParameter("softwareSize");
         String downloads=request.getParameter("downloads");
         String flatformId=request.getParameter("flatformId");
         String categoryLevel1=request.getParameter("categoryLevel1");
         String categoryLevel2=request.getParameter("categoryLevel2");
         String categoryLevel3=request.getParameter("categoryLevel3");
         String appInfo=request.getParameter("appInfo");

         AppInfo info=new AppInfo();
         info.setId(Long.parseLong(id));
         info.setSoftwarename(softwareName);
         info.setApkname(APKName);
         info.setSupportrom(supportROM);
         info.setInterfacelanguage(interfaceLanguage);
         info.setSoftwaresize(BigDecimal.valueOf(Double.valueOf(softwareSize)));
         info.setAppinfo(appInfo);
         info.setFlatformid(Long.parseLong(flatformId));
         info.setCategorylevel3(Long.parseLong(categoryLevel3));
         info.setDownloads(Long.parseLong(downloads));
         info.setCategorylevel1(Long.parseLong(categoryLevel1));
         info.setCategorylevel2(Long.parseLong(categoryLevel2));
         info.setLogolocpath(idPath);
         info.setLogopicpath(idPath);
         int i = AppInfoServiceDao.updateAInfo(info);
         return "redirect:/dev/flatform/app/list";
     }

     /*
     修改删除照片
      */
     @ResponseBody
     @RequestMapping(value = "/flatform/app/delfile.json")
     public Object delfile(HttpServletRequest request,Model model,HttpSession session){

         return JSONArray.toJSONString("success");
     }
/********************************************************************************************************************/
    /*
    新增版本页面
     */
    @RequestMapping(value = "/flatform/app/appversionadd")
    public String appversionadd(HttpServletRequest request,Model model,HttpSession session){
        String id = request.getParameter("id");
        List<AppVersion> appVersionList = AppVersionServiceDao.selectByAID(Integer.parseInt(id));
        model.addAttribute("appVersionList",appVersionList);
        model.addAttribute("appid",id);//用来版本修改时修改app信息的versionID字段
        return "developer/appversionadd";
    }
    /*
    新增版本操作
     */
    @RequestMapping(value="/flatform/app/addversionsave",method= RequestMethod.POST)
    public String addversionsave(HttpServletRequest request,Model model,
                                    @RequestParam(value ="idPath", required = false)MultipartFile attach) throws ParseException {
        String idPath=null;
        String fileName=null;
        if(!attach.isEmpty()){
            //statics固定名称
            String path = request.getSession().getServletContext().getRealPath("statics"+File.separator+"uploadfiles");
            String oldFileName = attach.getOriginalFilename();//原文件名
            String prefix= FilenameUtils.getExtension(oldFileName);
            int filesize = 500000;

            if(attach.getSize()>filesize){
                return "developer/appversionadd";
            }else if(prefix.equalsIgnoreCase("jpg")||prefix.equalsIgnoreCase("png")
                    ||prefix.equalsIgnoreCase("jpeg")||prefix.equalsIgnoreCase("pneg")){
                fileName = System.currentTimeMillis()+ RandomUtils.nextInt(1000000)+"_Personal.jpg";
                File targetFile = new File(path, fileName);
                if(!targetFile.exists()){
                    targetFile.mkdirs();
                }
                //保存
                try {
                    attach.transferTo(targetFile);
                    System.out.println("上传成功");
                } catch (Exception e) {
                    e.printStackTrace();
                    request.setAttribute("uploadFileError", " * 上传失败！");
                    return "useradd";
                }
                /*idPath = path+ File.separator+fileName;
                System.out.println(idPath);*/
                idPath ="statics"+"/"+"uploadfiles"+"/"+fileName;
            }else{
                request.setAttribute("uploadFileError", " * 上传图片格式不正确");
                return "developer/appversionadd";
            }

            String appId=request.getParameter("appId");
            String versionNo=request.getParameter("versionNo");
            String versionSize=request.getParameter("versionSize");
            String versionInfo=request.getParameter("versionInfo");
            Date date = new Date();//给了一个当前系统时间为最新更新时间
            SimpleDateFormat sdf = new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
            String nowTime = sdf.format(date);
            Date time = sdf.parse(nowTime);

            AppVersion appVersion=new AppVersion();
            appVersion.setAppid(Long.parseLong(appId));
            appVersion.setVersionno(versionNo);
            appVersion.setVersionsize(BigDecimal.valueOf(Double.valueOf(versionSize)));
            appVersion.setVersioninfo(versionInfo);
            appVersion.setDownloadlink(idPath);
            appVersion.setApklocpath(idPath);
            appVersion.setCreationdate(time);
            appVersion.setApkfilename(fileName);
            int i = AppVersionServiceDao.insertAVInfo(appVersion);
            if (i>0){
                System.out.println("-----------------------------------------------------------睡觉啊");
                AppInfoServiceDao.updateVIDByAID(Integer.parseInt(appId));
            }
            return "redirect:/dev/flatform/app/list";
        }else{
            System.out.println("他是空的");
            return "developer/appversionadd";
        }

    }

   /*********************************************************************************************************************/
    /*
    修改版本页面
     */
    @RequestMapping(value = "/flatform/app/appversionmodify")
    public String appversionmodify(HttpServletRequest request,Model model,HttpSession session){
        String aid = request.getParameter("aid");
        String vid = request.getParameter("vid");
        List<AppVersion> appVersionList = AppVersionServiceDao.selectByAID(Integer.parseInt(aid));
        AppVersion appVersion = AppVersionServiceDao.selectAVById(Integer.parseInt(vid));

        model.addAttribute("appVersionList",appVersionList);
        model.addAttribute("appVersion",appVersion);
        return "developer/appversionmodify";
    }

    /*
    修改版本操作
     */
    @RequestMapping(value="/flatform/app/appversionmodifysave",method= RequestMethod.POST)
    public String appversionmodifysave(HttpServletRequest request,Model model,
                                 @RequestParam(value ="idPath", required = false)MultipartFile attach) throws ParseException {
        String idPath=null;
        String aid=request.getParameter("appId");
        String vid=request.getParameter("id");
        if(!attach.isEmpty()){
            //statics固定名称
            String path = request.getSession().getServletContext().getRealPath("statics"+File.separator+"uploadfiles");
            String oldFileName = attach.getOriginalFilename();//原文件名
            String prefix= FilenameUtils.getExtension(oldFileName);
            int filesize = 500000;

            if(attach.getSize()>filesize){
                return "redirect:/dev/flatform/app/appversionmodify?aid="+aid+"&vid="+vid;
            }else if(prefix.equalsIgnoreCase("jpg")||prefix.equalsIgnoreCase("png")
                    ||prefix.equalsIgnoreCase("jpeg")||prefix.equalsIgnoreCase("pneg")){
                String fileName = System.currentTimeMillis()+ RandomUtils.nextInt(1000000)+"_Personal.jpg";
                File targetFile = new File(path, fileName);
                if(!targetFile.exists()){
                    targetFile.mkdirs();
                }
                //保存
                try {
                    attach.transferTo(targetFile);
                    System.out.println("上传成功");
                } catch (Exception e) {
                    e.printStackTrace();
                    request.setAttribute("uploadFileError", " * 上传失败！");
                    return "useradd";
                }
                /*idPath = path+ File.separator+fileName;
                System.out.println(idPath);*/
                idPath ="statics"+"/"+"uploadfiles"+"/"+fileName;
            }else{
                request.setAttribute("uploadFileError", " * 上传图片格式不正确");
                return "redirect:/dev/flatform/app/appversionmodify?aid="+aid+"&vid="+vid;
            }

        }
        String versionNo=request.getParameter("versionNo");
        String versionSize=request.getParameter("versionSize");
        String versionInfo=request.getParameter("versionInfo");
        Date date = new Date();//给了一个当前系统时间为最新更新时间
        SimpleDateFormat sdf = new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
        String nowTime = sdf.format(date);
        Date time = sdf.parse(nowTime);

        AppVersion appVersion=new AppVersion();
        appVersion.setId(Long.parseLong(vid));
        appVersion.setVersionno(versionNo);
        appVersion.setVersionsize(BigDecimal.valueOf(Double.valueOf(versionSize)));
        appVersion.setVersioninfo(versionInfo);
        appVersion.setDownloadlink(idPath);
        appVersion.setApklocpath(idPath);
        appVersion.setModifydate(time);
        int i = AppVersionServiceDao.updateAVInfo(appVersion);
        if (i>0){
            System.out.println("-----------------------------------------------------------睡觉啊");
        }
        return "redirect:/dev/flatform/app/list";

    }

    /******************************************************************************************************************/
    /*
    下载
     */
    @RequestMapping(value = "/flatform/app/download")
    public void downAppApk(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String apkName=request.getParameter("apkFileName");

        String fileName = request.getSession().getServletContext().getRealPath("statics/uploadfiles")+"/"+apkName;

        InputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName)));

        apkName = URLEncoder.encode(apkName,"UTF-8");

        response.addHeader("Content-Disposition", "attachment;filename=" + apkName);

        response.setContentType("multipart/form-data");

        BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
        int len = 0;
        while((len = bis.read()) != -1){
            out.write(len);
            out.flush();
        }
        out.close();
    }
}
